﻿/* Write a program that reads a text file containing a list of strings, 
 * sorts them and saves them to another text file. Example:
	Ivan			George
	Peter			Ivan
	Maria			Maria
	George			Peter
*/
using System;
using System.Collections.Generic;
using System.IO;

namespace _6.SortNames
{
    class SortNames
    {
        static void Main()
        {
            Console.Write("Path to input file (the file containning the names): ");
            string path_to_input_file = Console.ReadLine();

            Console.Write("Path to output file (the file to which will be written the output result): ");
            string path_to_output_file = Console.ReadLine();

            string line = null;
            List<string> names = new List<string>();
            try
            {
                StreamReader SR = new StreamReader(@path_to_input_file);
                line = SR.ReadLine();
                using (SR)
                {
                    while (line != null)
                    {
                        names.Add(line);
                        line = SR.ReadLine();
                    }
                }
            }
            catch (FileNotFoundException fn)
            {
                throw new FileNotFoundException("File not found, " + fn.Message);
            }
            catch (OverflowException oe)
            {
                throw new OverflowException("Stack overflow, may be the file is too big?, " + oe.Message);
            }
            catch (IOException ioe)
            {
                throw new IOException("Error: the input file could not be read, " + ioe.Message);
            }

            //sort names
            names.Sort();

            //write to output file
            try
            {
                StreamWriter SW = new StreamWriter(@path_to_output_file);
                using (SW)
                {
                    foreach (var v in names)
                    {
                        //Console.WriteLine(v);
                        SW.WriteLine(v);
                    }
                }

            }
            catch (FileNotFoundException fn)
            {
                throw new FileNotFoundException("File not found, " + fn.Message);
            }
            catch (OverflowException oe)
            {
                throw new OverflowException("Stack overflow, may be the file is too big?, " + oe.Message);
            }
            catch (IOException ioe)
            {
                throw new IOException("Error: the input file could not be read, " + ioe.Message);
            }
            Console.WriteLine("Data succesfully written to " + path_to_output_file);
        }
    }
}
